Collection View

  • A Collection View manages the presentation of items, your app’s main job is to provide the data that those items are to represent.

    A collection view gets its data from the data source, which is an object that conforms to the CollectionViewDataSource protocol. Data provided by the data source is represented items, which can also be organized into sections, and ultimately displayed as cells.

    Gathings Data

    The data source only has 3 requirements, provide the number of sections, the number of items, and a cell for each item. For performance, cells in a collection view are reusable since only a subset of all the items will often be visbile.

    Before you can load a cell, you need to register the cells you will need to represet your data. Cells can be registered from a nib or from a class using register(class:forCellWithReuseIdentifier:) or register(nib:forCellWithReuseIdentifier:). The reuse identifier will later be use to get instances of the cell.

    To create the cells for each item in your data, implement the data source method func collectionView(_:cellForItemAt:) -> CollectionViewCell. In here you will call dequeueReusableCell(withReuseIdentifier:for:) which will load an instance of the cell your previously registered for that resuse identifier. After you have dequeued the cell, update it as needed to properly represent the object at the given index path in your data, then return it.

    Laying Out Items

    After the data source has provided all the cells to be displayed, the collection view looks to its CollectionViewLayout to determine where to place each one. The base layout object is designed to be subclassed to generate layout information for different use cases. The goal of a layout is to be able to provide information about the layout to the collection view quickly, this inlcudes the location of each cell, the overall size of all the items, etc.

    The following layouts are provided for common uses, for more custom layouts, create a custom CollectionViewLayout subclass.

    • CollectionViewColumnLayout
    • CollectionViewListLayout
    • CollectionViewFlowLayout
    • CollectionViewHorizontalLayout
    See more

    Declaration

    Swift

    open class CollectionView : ScrollView, NSDraggingSource
  • The CollectionViewDataSource is responsible for providing the data and views required by a collection view

    Overview

    At a minimum, all data source objects must implement:

    • numberOfSections(in:)
    • collectionView(_:numberOfItemsInSection:)
    • collectionView(_:cellForItemAt:).

    These methods are responsible for returning the number of items in the collection view along with the items themselves.

    See more

    Declaration

    Swift

    @objc
    public protocol CollectionViewDataSource
  • The CollectionViewDelegate protocol defines methods that allow you to manage the status, selection, highlighting, and scrolling of items in a collection view and to perform actions on those items. The methods of this protocol are all optional.

    See more

    Declaration

    Swift

    @objc
    public protocol CollectionViewDelegate
  • The CollectionViewDragDelegate forwards system drag functions to the delegate in the context of a Collection View.

    See more

    Declaration

    Swift

    @objc
    public protocol CollectionViewDragDelegate : CollectionViewDelegate
  • A CollectionViewCell object presents the content for a single data item when that item is within the collection view’s visible bounds. You can use this class as-is or subclass it to add additional properties and methods. The layout and presentation of cells is managed by the collection view and its corresponding layout object.

    See more

    Declaration

    Swift

    open class CollectionViewCell : CollectionReusableView
  • The CollectionReusableView class defines the behavior for all cells and supplementary views presented by a collection view. Reusable views are so named because the collection view places them on a reuse queue rather than deleting them when they are scrolled out of the visible bounds. Such a view can then be retrieved and repurposed for a different set of content.

    See more

    Declaration

    Swift

    open class CollectionReusableView : NSView
  • CollectionElementCategory

    • cell:
    • supplementaryView:

    Declaration

    Swift

    public enum CollectionElementCategory
  • CollectionViewDirection

    • left:
    • right:
    • up:
    • down:

    Declaration

    Swift

    public enum CollectionViewDirection
  • CollectionViewScrollDirection

    • vertical:
    • horizontal:

    Declaration

    Swift

    public enum CollectionViewScrollDirection
  • CollectionViewScrollPosition

    • none:
    • nearest:
    • leading:
    • centered:
    • trailing:

    Declaration

    Swift

    public enum CollectionViewScrollPosition
  • AnimationCompletion

    Declaration

    Swift

    public typealias AnimationCompletion = (_ finished: Bool) -> Void
  • An easy to use CollectionViewController that transitions from a source collection view.

    Presentation & Data

    The controller is presented from a source collection view. The data source of the source collection view is used to load data for the preview collection view. The preview controller will act as a proxy between the preview collection view and your source colleciton views data source.

    Important

    The data source for the collection view passed to present(in:) must conform to CollectionViewPreviewControllerDelegate

    Transitions

    The preview controller manages the transitions to and from the source and allows the preview cell to customize the transition.

    Although the The preview controller will accept any cell class, supporting transitions requires a small amount of additional setup.

    The simplest way to support transitions is to create your subclass from CollectionViewPreviewCell. CollectionViewPreviewCell will animate the frame of the cell from the source and back.

    For custom transitions, if you subclass CollectionViewPreviewCell you can simply override the the transition methods, otherwise conform your CollectionViewCell subclass and implement the methods yourself. See CollectionViewPreviewTransitionCell for more about how to implement custom transitions

    See more

    Declaration

    Swift

    open class CollectionViewPreviewController : CollectionViewController, CollectionViewDelegatePreviewLayout
  • a CollectionViewPreviewControllerDelegate is responsible for providing data to a CollectionViewPreviewController.

    See more

    Declaration

    Swift

    public protocol CollectionViewPreviewControllerDelegate : AnyObject